﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.Services;
using Ranet.AgOlap.Controls.PivotGrid.ClientServer;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands.PivotGrid
{
    public class ToolBarInfoCommand : ServerCommand
    {
        public ToolBarInfoCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.ToolBarInfoCommandId)
        {
            this.ContextId = invoker[KnownInvokeArgs.ContextId];
            this.DataSourceId = invoker[KnownInvokeArgs.DataSourceId];
        }

        public string ContextId { get; private set; }
        public string DataSourceId { get; private set; }

        private IBindingContext Context
        {
            get
            {
                return DataBinder.Current.GetContext(this.ContextId);
            }
        }

        private OlapDataManager DataManager
        {
            get
            {
                IBindingManager bindingMgr = DataBinder.Current.GetBindingManager(this.ContextId, this.DataSourceId);
                if (bindingMgr != null)
                {
                    return bindingMgr.DataManager as OlapDataManager;
                }

                return null;
            }
        }

        public override Galaktika.BI.Runtime.Services.InvokeResultSchema Invoke()
        {
            try
            {
                InvokeResultSchema res = new InvokeResultSchema(base.Id);

                if (DataManager != null)
                {
                    PivotGridToolBarInfo toolBarInfo = new PivotGridToolBarInfo();
                    toolBarInfo.HistorySize = DataManager.History.Size;
                    toolBarInfo.CurrentHistoryIndex = DataManager.History.CurrentHistiryItemIndex;
                    toolBarInfo.HideEmptyRows = DataManager.HideEmptyRows;
                    toolBarInfo.HideEmptyColumns = DataManager.HideEmptyColumns;

                    res.Content = XmlUtility.Obj2XmlStr(toolBarInfo, Common.Namespace);
                    res.ContentType = KnownContentTypes.MultidimData;
                }

                return res;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
